package ro.gateway.aida.struts.action;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import ro.gateway.aida.obj.Organization;
import ro.gateway.aida.obj.db.OrganizationDB;
import ro.gateway.aida.struts.MultiTilesAction;
import ro.gateway.aida.usr.Arbiter;
import ro.gateway.aida.usr.Perms;
import ro.gateway.aida.usr.UserUtils;
import ro.gateway.aida.utils.PageToken;
import ro.gateway.aida.utils.Utils;

public class QuickAddAdminAction extends MultiTilesAction {
	public ActionForward modePrepare(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Arbiter ar = Arbiter.getManager(token);
		if (UserUtils.getLogged(request, response) == null)
			return mapping.findForward("plslogin");
		if (!ar.canDo(request, Perms.ADMIN_TOOLS))
			mapping.findForward("norights");
		return modeSelect(mapping, form, request, response);
	}
	public ActionForward modeRefresh(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		OrganizationDB orgdb = OrganizationDB.getManager(token);
		HttpSession session = request.getSession();
		session.setAttribute(ORG_LIST, orgdb.getOrgTemp());
		return modeList(mapping, form, request, response);
	}
	public ActionForward modeEdit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		OrganizationDB orgdb = OrganizationDB.getManager(token);
		HttpSession session = request.getSession();
		session.setAttribute(Organization.EDIT_TAG,orgdb.getByID(
				Long.parseLong(request.getParameter("edit_id"))));
		Utils.setBreadCrumbs(request, "bc.quickaddedit");
		return mapping.findForward("quickadd.edit");
	}
	public ActionForward modeList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		List sl = (List) session.getAttribute(ORG_LIST);
		PageToken.tokenizeList(request, sc, sl, "orgs");
		Utils.setBreadCrumbs(request, "bc.quickaddman");
		return mapping.findForward("quickadd.list");
	}
	
	public ActionForward modeQuickSelect(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		List sl = (List) session.getAttribute(ORG_LIST);
		PageToken.tokenizeList(request, sc, sl, "orgs");
		List orgs=(List)session.getAttribute("SelectedOrgs");
		Organization selected=(Organization)orgs.iterator().next();
		if(selected==null) return modeEdit(mapping,form,request,response);
		request.setAttribute("selected",selected);
		return mapping.findForward("quickadd.edit");
	}
	
	public ActionForward modeQuickLink(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		OrganizationDB orgdb = OrganizationDB.getManager(token);
		long tempId=Long.parseLong(request.getParameter("edit_id"));
		long linkId=Long.parseLong(request.getParameter("link_id"));
		
		orgdb.linkOrgs(tempId,linkId);
		orgdb.delete(tempId);
		
		return modeRefresh(mapping, form, request, response);
	}
	
	
	public ActionForward modeSelect(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		if (session.getAttribute(ORG_LIST) == null
				|| request.getParameter("refresh") != null)
			return modeRefresh(mapping, form, request, response);
		if(request.getParameter("quicklink")!=null) 			
			return modeQuickLink(mapping, form, request, response);
		if(request.getParameter("quickselect")!=null) 			
			return modeQuickSelect(mapping, form, request, response);
		if (request.getParameter("edit_id") != null)
			return modeEdit(mapping, form, request, response);
		
		
		return modeList(mapping, form, request, response);
	}
	public static final String ORG_LIST = "Organization_TEMP_LIST";
	
}